﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Animation
{
    public class SpriteType
    {
        public string Name { get; set; }
        public string IconAsset { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public string CollisionData { get; set; }
        public string BotName { get; set; }
        public List<AnimationState> StateList { get; set; }

        [ContentSerializerIgnore]
        public Dictionary<string, AnimationState> States { get; set; }
        [ContentSerializerIgnore]
        public Texture2D Icon { get; set; }
        public string CurrentState {
            get
            {
                return currentState;
            }
            set
            {
                if (!value.Equals(currentState))
                {
                    currentState = value;
                    if (currentState != null)
                    {
                        if (States.Count > 0)
                            States[currentState].Reset();
                    }
                }
                else
                    States[currentState].Reset();
            }
        }
        [ContentSerializerIgnore]
        private string currentState;

        public Texture2D GetNextFrame()
        {
            return States[CurrentState].GetNextFrame();
        }

        public SpriteType()
        {
            States = new Dictionary<string, AnimationState>();
        }

        public SpriteType Clone()
        {
            SpriteType t = new SpriteType();

            t.CurrentState = CurrentState;

            List<AnimationState> newStateList = new List<AnimationState>();
            
            Dictionary<string, AnimationState> newStates = new Dictionary<string, AnimationState>();

            foreach (AnimationState s in StateList)
            {
                AnimationState newState = s.Clone();
                newStateList.Add(newState);
                newStates[newState.StateName] = newState;
            }

            t.States = newStates;
            t.StateList = newStateList;
            t.BotName = this.BotName;
            t.CollisionData = this.CollisionData;
            t.IconAsset = this.IconAsset;
            t.Icon = this.Icon;
            t.Width = this.Width;
            t.Height = this.Height;
            t.Name = this.Name;

            return t; 
        }


    }

    public class SpriteTypeContentReader : ContentTypeReader<SpriteType>
    {
        protected override SpriteType Read(ContentReader input, SpriteType existingInstance)
        {
            SpriteType s = new SpriteType();

            s.Name = input.ReadString();
            s.IconAsset = input.ReadString();
            s.Width = input.ReadInt32();
            s.Height = input.ReadInt32();
            s.CollisionData = input.ReadString();
            s.BotName = input.ReadString();
            s.Icon = input.ContentManager.Load<Texture2D>(s.IconAsset);

            s.StateList = input.ReadObject<List<AnimationState>>();
            s.CurrentState = input.ReadString();
            
            foreach (AnimationState state in s.StateList)
            {
                s.States[state.StateName] = state;
            }

            return s;
        }
    }
}
